﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.Product
{
    public partial class EditProductInfoForm : BasicPage
    {
        public int Id;

        private readonly ProductBLL _bll = new ProductBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"] as string, out this.Id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                chkLoginLevel("editProduct");
                ShowInfo(this.Id);
            }
        }

        private void ShowInfo(int id)
        {
            var model = _bll.GetProductInfo(id);
            txtProductName.Text = model.ProductName;
            txtProductTitle.Text = model.ProductTitle;
            txtProductIcon.Text = model.ProductIcon;
            txtProductImg.Text = model.ProductImg;
            FCKeditor.Value = model.ProductContent;
            txtOrderId.Text = model.OrderId.ToString();
            if (model.Status == 1)
            {
                cblItem.Items[0].Selected = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new ProductInfo();
            model.PId = this.Id;
            model.ProductName = txtProductName.Text.Trim();
            model.ProductTitle = txtProductTitle.Text.Trim();
            model.ProductContent = FCKeditor.Value;
            model.ProductIcon = txtProductIcon.Text.Trim();
            model.ProductImg = txtProductImg.Text.Trim();
            model.OrderId = int.Parse(txtOrderId.Text.Trim());
            model.Status = 0;
            if (cblItem.Items[0].Selected)
            {
                model.Status = 1;
            }
            model.ModifyUser = "李波";
            model.ModifyTime = DateTime.Now;
            if (_bll.Update(model))
            {
                JscriptPrint("修改成功！", "ProductMasterForm.aspx", "Success");
            }
            else
            {
                JscriptPrint("修改失败！", "", "Error");
            }
        }
    }
}